home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9183 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.0 KB  |  111 lines

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Big Endian - Little Endian ?
  5. Date: 8 Mar 1996 09:12:05 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4hppp5INNr2@keats.ugrad.cs.ubc.ca>
  8. References: <DnxL26.H7o@mail.auburn.edu>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <DnxL26.H7o@mail.auburn.edu>,
  12. Bruce E. Tucker <betucker@eng.auburn.edu> wrote:
  13. >
  14. >I am in need of a Big Endian - Little Endian conversion utility in C
  15. >for floating point numbers.
  16.  
  17. Sun Microsystems developed a standard known as XDR. There is a freely
  18. distributed version of it floating around.
  19.  
  20. The XDR library provides filter routines for encoding basic C data types into
  21. external representation. It also provides an abstract data type known as an
  22. ``XDR stream'', through which serializing and deserializing operations take
  23. place. A stream can be initialized to encode/decode to a memory buffer,
  24. standard IO stream, or TCP/IP connection.
  25.  
  26. There is also a processor known as ``rpcgen'' which translates higher-order
  27. structure and union definitions into recursive encoding/decoding filter
  28. routines which use XDR primitives to do wholesale encoding of structures. This
  29. interface compiler also can generate RPC stub routines and a server skeleton
  30. for making a simple distributed application. You just fill in the
  31. functionality. Though you probably just want the XDR capability which can
  32. easily be used on its own.
  33.  
  34. In a recent project, my team has ported XDR successfully to 16-bit Windows.
  35. It's widely available on UNIX platforms---generally, wherever you find Sun
  36. NFS/NIS stuff, you will find XDR.
  37.  
  38. >I have a file full of Big Endian floats and doubles and I need to be
  39. >able to convert these numbers for use on a Little Endian machine.
  40.  
  41. If these are in IEEE format, the file could be directly treated as an XDR input
  42. stream, since the XDR canonical format for floating-point numbers is big-endian
  43. IEEE. It's possible that XDR was even used to create the file.
  44.  
  45. Here is what a program to decode the file might look like:
  46.  
  47.  
  48. #include <rpc/rpc.h>    /* this includes a lot of standard headers    */
  49.  
  50. int main(int argc, char *argv[])
  51.  
  52. {
  53.     FILE *f = fopen("datafile","r");
  54.     XDR xstream;
  55.  
  56.     double data;
  57.  
  58.     if (!f) {
  59.         puts("couldn't open file");
  60.         exit(1);
  61.     }
  62.  
  63.     xdrstdio_create(&xstream, f, XDR_DECODE);
  64.  
  65.     while(xdr_double(&xtream,&data) == TRUE) {
  66.         /* process data                */
  67.         /* TRUE is defined in <rpc/types.h>    */
  68.     }
  69.  
  70.     xdr_destroy(&xtream);    /* calls fflush(), not fclose()    */
  71.     fclose(f);
  72.  
  73.     return 0;
  74. }
  75.  
  76.  
  77. >Any tips, pointers, or programming examples would be appreciated.
  78. >Thanks in advance !
  79.  
  80. Shrug. I guess my response is a little off=topic for this newsgroup. 
  81.  
  82.  
  83. By the way, I just checked the FAQ. It does not mention Sun XDR and other
  84. techniques, though it does not address the specific problem of decoding an
  85. arbitrary data file, but the problem of creating a portable data file:
  86.  
  87. 20.5:   How can I write data files which can be read on other machines
  88.         with different word size, byte order, or floating point formats?
  89.  
  90. A:      The most portable solution is to use text files (usually ASCII),
  91.         written with fprintf() and read with fscanf() or the like.
  92.         (Similar advice also applies to network protocols.)  Be
  93.         skeptical of arguments which imply that text files are too big,
  94.         or that reading and writing them is too slow.  Not only is their
  95.         efficiency frequently acceptable in practice, but the advantages
  96.         of being able to interchange them easily between machines, and
  97.         manipulate them with standard tools, can be overwhelming.
  98.  
  99.         If you must use a binary format, you can improve portability,
  100.         and perhaps take advantage of prewritten I/O libraries, by
  101.         making use of standardized formats such as Sun's XDR (RFC 1014),
  102.         OSI's ASN.1 (referenced in CCITT X.409 and ISO 8825 "Basic
  103.         Encoding Rules"), CDF, netCDF, or HDF.  See also questions 2.12
  104.         and 12.38.
  105.  
  106.         References: PCS Sec. 6 pp. 86,88.
  107.  
  108.  
  109. -- 
  110.  
  111.